#ifndef LIBTORRENT_STORAGE_H
#define LIBTORRENT_STORAGE_H

#include <sys/stat.h>
#include "storage_block.h"
#include <cstdint>
#include <string>
#include <vector>

namespace torrent {

class Storage {
	typedef std::vector<StorageBlock::Block*> BlockVector;

public:
	Storage();
	~Storage();

	bool open(const std::string& filename,
				unsigned int blockSize, unsigned int offset,
				bool writeAccess, bool createFile, unsigned int mask = 0644);
	void close();

	// does not guarantee write access.
	bool isWrite() const;
	bool isOpen() const;

	uint64_t fileSize() const;
	unsigned int blockCount() const;

private:
	void clear();

	int m_fd;
	unsigned int m_blockSize;
	unsigned int m_offset;

	struct stat m_stat;
	bool m_write;

	BlockVector m_blocks;
};

} // namespace torrent

#endif // LIBTORRENT_STORAGE_H
